/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FileListForm.java
 *
 * Created on 31.05.2010, 20:54:58
 */

package shaman;


import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

class FileTableModel extends AbstractTableModel
{

    private class Row
    {
        public Row(String file, boolean checked)
        {
            this.file = file;
            this.checked = checked;
        }

        public boolean getChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String getFile() {
            return file;
        }

        public void setFile(String file) {
            this.file = file;
        }
        private String file;
        private boolean checked;

    }

    List<Row> files = new ArrayList<Row>();
    boolean showCheck = false;

    public FileTableModel(List<String> files, boolean showCheck) {
        this.showCheck = showCheck;
        for (String fn: files)
            this.files.add(new Row(fn, true));
    }

    public int getRowCount() {
        return files != null ? files.size() : 0;
    }

    public int getColumnCount() {
        return showCheck ? 2 : 1;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex)
        {
            case 0: {
                return showCheck ?
                        files.get(rowIndex).getChecked() :
                        files.get(rowIndex).getFile();
            }
            case 1: {
                return showCheck ?
                        files.get(rowIndex).getFile() :
                        null;
            }
            default: return null;
        }
    }
}

/**
 *
 * @author shaman
 */
public class FileListForm extends javax.swing.JFrame {

    public FileListForm() {
        initComponents();
    }

    /** Creates new form FileListForm */
    public FileListForm(List<String> files, boolean showCheck, String text, String title) {
        initComponents();
        jFileTable.setModel(new FileTableModel(files, showCheck));
        jLabelText.setText(text);
        this.setTitle(title);
        jLabelInfo.setText("Всего: "+String.valueOf(files.size()) + " файлов");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jFileTable = new javax.swing.JTable();
        jLabelText = new javax.swing.JLabel();
        jbOK = new javax.swing.JButton();
        jLabelInfo = new javax.swing.JLabel();

        setAlwaysOnTop(true);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jFileTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jFileTable.setName("jFileTable"); // NOI18N
        jScrollPane1.setViewportView(jFileTable);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(shaman.RegBackupApp.class).getContext().getResourceMap(FileListForm.class);
        jLabelText.setText(resourceMap.getString("jLabelText.text")); // NOI18N
        jLabelText.setName("jLabelText"); // NOI18N

        jbOK.setText(resourceMap.getString("jbOK.text")); // NOI18N
        jbOK.setName("jbOK"); // NOI18N
        jbOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOKActionPerformed(evt);
            }
        });

        jLabelInfo.setText(resourceMap.getString("jLabelInfo.text")); // NOI18N
        jLabelInfo.setName("jLabelInfo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 544, Short.MAX_VALUE)
                .addComponent(jLabelInfo)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(508, Short.MAX_VALUE)
                .addComponent(jbOK, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 611, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelText)
                    .addComponent(jLabelInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbOK)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOKActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jbOKActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FileListForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable jFileTable;
    private javax.swing.JLabel jLabelInfo;
    private javax.swing.JLabel jLabelText;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbOK;
    // End of variables declaration//GEN-END:variables

}
